<template>
	<view class="page">
		<view class="chatList" v-if="isShow">
			<UserItem v-for="item in list" :key="item.id" :info="item" :isPerple="isPerple"></UserItem>
		</view>
		<view class="chatList" v-else>
			<view class="station" v-for="(item, i) in list2" :key="i">
				<view class="name">站点名：{{item.name}}</view>
				<view class="leader">负责人：{{item.leader}}</view>
				<view class="phone">联系电话：{{item.phone}}</view>
				<view class="place">地址：{{item.place}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import UserItem from '../../components/UserItem/UserItem.vue'
	import {getGroupList, getMember, getChatList} from '../../network/chat.js'
	import {setGather} from '../../utils/index.js'
	import {
		getStationList,
	} from '../../network/home.js'
	export default {
		components: {
			UserItem
		},
		data() {
			return {
				isShow: true,
				isPerple: false,
				list: {},
				list2: [],
			};
		},
		onShow() {
			console.log(this.$store.state.userInfo)
			this.isShow = this.$store.state.userInfo.role != "家属"
			if (this.isShow) {
				this.list = this.$store.state.gather
				this.isPerple = false;
				return void this.init()
			} 
			this.relationChatList()
		},
		methods: {
			async init() {
				if (Object.keys(this.list).length == 0) {
					await setGather()
					this.list = this.$store.state.gather
			console.log(this.list)
				}
			},
			
			async relationChatList() {
				const result = await getChatList()
				if (!result.flag || result.data.length <= 0) {
					return void this.station()
				}
				this.isShow = true
				this.isPerple = true;
				let o = {}
				result.data.forEach(item => {
					o[item[0].phone] = {...item[0], number: 0}
				});
				this.$store.commit('setGather', o)
				console.log(this.$store.state.gather)
				this.list = o
			},
			
			async station() {
				this.isActive = false;
				const result = await getStationList()
				console.log(result)
				if (!result.flag) {
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					})
					return
				}
				this.list2 = result.data.slice()
			},
		}
	}
</script>

<style lang="scss" scoped>
.page {
	position: absolute;
	width: 100%;
	height: 100%;
	background-color: white;
	.chatList {
		background-color: $uni-bg-color;
		padding-bottom: 70px;
		.station {
			background-color: white;
			margin: 10px 0;
			padding: 10px;
			border-bottom: 8px solid #F2F2F2;
			view {
				margin: 5px 0;
			}
			.lost {
				margin-top: 10px;
				font-size: 12px;
				color: #1CBAB6;
				text-align: center;
			}
		}
	}
}
</style>
